package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.HouseOrder;
import com.ruoyi.system.mapper.HouseOrderMapper;
import com.ruoyi.system.service.IHouseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房源订购Service业务层处理
 * 
 * @author ...
 * @date 2022-03-18
 */
@Service
public class HouseOrderServiceImpl implements IHouseOrderService 
{
    @Autowired
    private HouseOrderMapper houseOrderMapper;

    /**
     * 查询房源订购
     * 
     * @param id 房源订购主键
     * @return 房源订购
     */
    @Override
    public HouseOrder selectHouseOrderById(Integer id)
    {
        return houseOrderMapper.selectHouseOrderById(id);
    }

    /**
     * 查询房源订购列表
     * 
     * @param houseOrder 房源订购
     * @return 房源订购
     */
    @Override
    public List<HouseOrder> selectHouseOrderList(HouseOrder houseOrder)
    {
        return houseOrderMapper.selectHouseOrderList(houseOrder);
    }

    /**
     * 新增房源订购
     * 
     * @param houseOrder 房源订购
     * @return 结果
     */
    @Override
    public int insertHouseOrder(HouseOrder houseOrder)
    {
        houseOrder.setCreateTime(DateUtils.getNowDate());
        return houseOrderMapper.insertHouseOrder(houseOrder);
    }

    /**
     * 修改房源订购
     * 
     * @param houseOrder 房源订购
     * @return 结果
     */
    @Override
    public int updateHouseOrder(HouseOrder houseOrder)
    {
        houseOrder.setUpdateTime(DateUtils.getNowDate());
        return houseOrderMapper.updateHouseOrder(houseOrder);
    }

    /**
     * 批量删除房源订购
     * 
     * @param ids 需要删除的房源订购主键
     * @return 结果
     */
    @Override
    public int deleteHouseOrderByIds(Integer[] ids)
    {
        return houseOrderMapper.deleteHouseOrderByIds(ids);
    }

    /**
     * 删除房源订购信息
     * 
     * @param id 房源订购主键
     * @return 结果
     */
    @Override
    public int deleteHouseOrderById(Integer id)
    {
        return houseOrderMapper.deleteHouseOrderById(id);
    }

    @Override
    public List<HouseOrder> findMyOrder() {
        int userId = SecurityUtils.getUserId().intValue();
        return houseOrderMapper.findMyOrder(userId);
    }

    @Override
    public Map<String, Integer> manageCount() {
        List<HouseOrder> faultManages = houseOrderMapper.selectHouseOrderList(new HouseOrder());
        Map<String, Integer> map = new HashMap<>();
        if (faultManages.size() > 0) {
            // 今日
            DateTime date = DateUtil.date();
            int today = (int) faultManages.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.isSameDay(item.getCreateTime(), date)).count();
            map.put("today", today);
            // 本周
            int thisWeek = DateUtil.weekOfYear(date);
            int week = (int) faultManages.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.weekOfYear(item.getCreateTime()) == thisWeek).count();
            map.put("week", week);
            // 本月
            int month = (int) faultManages.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.isSameMonth(item.getCreateTime(), date)).count();
            map.put("month", month);
            // 今年
            int thisYear = DateUtil.year(date);
            int year = (int) faultManages.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.year(item.getCreateTime()) == thisYear).count();
            map.put("year", year);
        }
        return map;
    }

    @Override
    public Map<String, Integer> startCount() {
        List<HouseOrder> orderList = houseOrderMapper.selectHouseOrderList(new HouseOrder());
        Map<String, Integer> map = new HashMap<>();
        if (orderList.size() > 0) {
            // 今日
            DateTime date = DateUtil.date();
            int todayPrice= orderList.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.isSameDay(item.getCreateTime(), date)).mapToInt(item -> Integer.parseInt(item.getTotalMoney())).sum();
            map.put("today", todayPrice);
            // 本周
            int thisWeek = DateUtil.weekOfYear(date);
            int weekPrice =  orderList.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.weekOfYear(item.getCreateTime()) == thisWeek).mapToInt(item -> Integer.parseInt(item.getTotalMoney())).sum();
            map.put("week", weekPrice);
            // 本月
            int monthPrice = orderList.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.isSameMonth(item.getCreateTime(), date)).mapToInt(item -> Integer.parseInt(item.getTotalMoney())).sum();
            map.put("month", monthPrice);
            // 今年
            int thisYear = DateUtil.year(date);
            int yearPrice =  orderList.parallelStream().filter(item -> item.getCreateTime() != null && DateUtil.year(item.getCreateTime()) == thisYear).mapToInt(item -> Integer.parseInt(item.getTotalMoney())).sum();
            map.put("year", yearPrice);
        }
        return map;
    }
}
